package com.wxw.BaseLibrary.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.wxw.BaseLibrary.component.BorderTextView;
import com.wxw.BaseLibrary.component.SimpleTextItemView;
import com.wxw.BaseLibrary.component.SimpleTextItemWithShadowView;
import com.wxw.BaseLibrary.component.SpinnerView;
import com.wxw.BaseLibrary.drawable.PanelStyle;
import com.wxw.BaseLibrary.element.IListItem;
import com.wxw.BaseLibrary.element.IObjectListAdapterAgent;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by david on 2014-06-06.
 */
public class ObjectListAdapter<T extends Object> extends BaseAdapter {
    private Context _context=null;
    private List<T> data = null;
    private IObjectListAdapterAgent agent = null;
    private boolean hasShadow = false;
    private boolean hasBorder = false;
    private boolean isSpinner = false;
    private float fontSize = 14;
    private int verticalMargin = 10;
    private int index = -1;

    public ObjectListAdapter(Context context, List<T> data){
        _context = context;
        this.data = data;
    }

    public ObjectListAdapter(Context context, IObjectListAdapterAgent agent, List<T> data) {
        _context = context;
        this.data = data;
        this.agent = agent;
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int i) {
        return data==null || i<0 || i>=data.size()?null:data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (data.size() <= i) {
            return null;
        }
        T t = data.get(i);
        if (agent != null) {
            convertView = getViewByAgent(i, convertView, t);
        } else {
            if (isSpinner) {
                if (convertView == null || !(convertView instanceof SpinnerView)) {
                    convertView = new SpinnerView(_context);
                }
                ((SpinnerView) convertView).setVerticalMargin(verticalMargin);
                ((SpinnerView) convertView).getTextView().setText(t.toString());
                ((SpinnerView) convertView).getTextView().setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
            } else {
                if (hasShadow) {
                    if (convertView == null || !(convertView instanceof SimpleTextItemWithShadowView)) {
                        convertView = new SimpleTextItemWithShadowView(_context);
                    }
                    ((SimpleTextItemWithShadowView) convertView).setVerticalMargin(verticalMargin);
                    ((SimpleTextItemWithShadowView) convertView).getTextView().setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
                    ((SimpleTextItemWithShadowView) convertView).getTextView().setText(t.toString());
//                if (convertView == null || !(convertView instanceof BorderTextView)){
//                    convertView = new BorderTextView(_context);
//                }
//                ((BorderTextView)convertView).setBorder_width(3);
//                ((BorderTextView)convertView).setBorder_color(Color.parseColor("#ffcb05"));
//                ((BorderTextView)convertView).setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
//                convertView.setBackgroundColor(Color.TRANSPARENT);
//                ((BorderTextView)convertView).setTextColor(Color.BLACK);
//                ((BorderTextView)convertView).setText(t.toString());
//                ((BorderTextView)convertView).setHasShadow(true);
//                ((BorderTextView)convertView).invalidate();
                } else {
                    if (convertView == null || !(convertView instanceof SimpleTextItemView)) {
                        convertView = new SimpleTextItemView(_context);
                        ((SimpleTextItemView) convertView).setHasBorder(hasBorder);
                    }
                    ((SimpleTextItemView) convertView).setVerticalMargin(verticalMargin);
                    ((SimpleTextItemView) convertView).getTextView().setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
                    ((SimpleTextItemView) convertView).getTextView().setText(t.toString());
                }
            }
        }
        if (i == index){
            convertView.setBackground(PanelStyle.createSkyBluePanelStyle());
        } else {
            if (t instanceof IListItem) {
                convertView.setBackgroundColor(((IListItem)t).getColor());
            } else {
                convertView.setBackgroundColor(Color.WHITE);
            }
        }
        return convertView;
    }

    public boolean hasShadow() {
        return hasShadow;
    }

    public void setHasShadow(boolean hasShadow) {
        this.hasShadow = hasShadow;
    }

    public boolean hasBorder() {
        return hasBorder;
    }

    public void setHasBorder(boolean hasBorder) {
        this.hasBorder = hasBorder;
    }

    public float getFontSize() {
        return fontSize;
    }

    public void setFontSize(float fontSize) {
        this.fontSize = fontSize;
    }

    public boolean isSpinner() {
        return isSpinner;
    }

    public void setSpinner(boolean isSpinner) {
        this.isSpinner = isSpinner;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    public int getVerticalMargin() {
        return verticalMargin;
    }

    public void setVerticalMargin(int verticalMargin) {
        this.verticalMargin = verticalMargin;
    }

    public IObjectListAdapterAgent getAgent() {
        return agent;
    }

    public void setAgent(IObjectListAdapterAgent agent) {
        this.agent = agent;
    }

    private View getViewByAgent(int i, View convertView, T t) {
        if (convertView == null) {
            convertView = LayoutInflater.from(_context).inflate(agent.getResourceID(), null);
        }
        agent.fillContent(convertView, t);
        return convertView;
    }
}
